<template>
  <div class="app-container">
    <span slot="label"><i class="el-icon-news"></i> 图文</span>
    <div class="add_but">
      <el-button type="primary"
                 @click="handleScanAdd"
                 v-hasPermi="['wxmp:wxconfig:add']"
                 size="mini">新 增
      </el-button>
    </div>
    <div class="waterfall" v-loading="loading">
      <el-row>
        <el-col :span="8" v-for="item in jwidList" :key="item.id" :offset="index > 0 ? 2 : 0" class="waterfall-item">
          <el-card :body-style="{ padding: '0px' }">
            <img :src="item.headimgurl" class="image">
            <div style="padding: 14px;">
              <span>{{item.name}}</span>
              <div class="bottom clearfix">
                <time class="time">{{ parseTime(item.tokenGettime, '{y}-{m}-{d}') }}</time>
                <el-row class="ope-row">
                  <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(item)"
                             v-hasPermi="['wxmp:wxconfig:remove']"></el-button>
                </el-row>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <div v-if="jwidList.length <=0 && !loading" class="el-table__empty-block">
      <span class="el-table__empty-text">暂无数据</span>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {listJwid, getJwid, delJwid, addJwid, updateJwid, exportJwid} from "@/api/wxmp/wxconfig";
  import {Base64} from 'js-base64';
  import {getWxAuthInfo} from "@/api/wxmp/wxauth";


  export default {
    name: "wxconfig",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 系统公众号表格数据
        jwidList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          jwid: null,
          name: null,
          applicationType: null,
          qrcodeimg: null,
          weixinNumber: null,
          weixinAppid: null,
          weixinAppsecret: null,
          accountType: null,
          authStatus: null,
          accessToken: null,
          tokenGettime: null,
          apiticket: null,
          apiticketGettime: null,
          jsapiticket: null,
          jsapiticketGettime: null,
          authType: null,
          businessInfo: null,
          funcInfo: null,
          nickName: null,
          headimgurl: null,
          authorizationInfo: null,
          authorizerRefreshToken: null,
          token: null,
          authorizationStatus: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          jwid: [
            {required: true, message: "公众号不能为空", trigger: "blur"}
          ],
          name: [
            {required: true, message: "名称不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询系统公众号列表 */
      getList() {
        this.loading = true;
        listJwid(this.queryParams).then(response => {
          this.jwidList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          jwid: null,
          name: null,
          applicationType: null,
          qrcodeimg: null,
          createBy: null,
          createTime: null,
          weixinNumber: null,
          weixinAppid: null,
          weixinAppsecret: null,
          accountType: null,
          authStatus: "0",
          accessToken: null,
          tokenGettime: null,
          apiticket: null,
          apiticketGettime: null,
          jsapiticket: null,
          jsapiticketGettime: null,
          authType: null,
          businessInfo: null,
          funcInfo: null,
          nickName: null,
          headimgurl: null,
          authorizationInfo: null,
          authorizerRefreshToken: null,
          token: null,
          authorizationStatus: "0"
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加系统公众号";
      },
      /** 扫码授权操作*/
      handleScanAdd() {
        console.log("getWxAuthInfo")
        getWxAuthInfo({
          base_api: process.env.VUE_APP_BASE_API
        })
          .then(response => {
            //跳转到授权页面
            const href = Base64.encode(response.data.url);//将地址URL base64加密方便传输
            console.log("handleScanAdd url: ")
            console.log(response.data.url)

            console.log(href)

            //跳转到新的页面
            window.open("/wxmp/wxauth?url=" + href, '_blank')
          });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getJwid(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改系统公众号";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateJwid(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addJwid(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除系统公众号编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delJwid(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有系统公众号数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportJwid(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style>
  .add_but {
    padding: 10px;
  }

  .waterfall-item {
    width: 20%;
    margin-left: 10px;
    margin-right: 10px;
  }

  .ope-row {
    margin-top: 5px;
    text-align: center;
    border-top: 1px solid #eaeaea;
    padding-top: 5px;
  }

  .time {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }

  .image {
    width: 100%;
    display: block;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }
</style>
